package cn.ks.web.controller;

import cn.ks.feign.GoodsService;
import cn.ks.service.ILssueService;
import cn.ks.domain.Lssue;
import cn.ks.query.LssueQuery;
import cn.ks.result.JSONResult;
import cn.ks.result.PageList;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/lssue")
public class LssueController {

    @Autowired
    public ILssueService lssueService;

    @Value("${number}")
    private int number;

     @Autowired
     private GoodsService goodsService;

    /**
    * 保存和修改公用的
    */
    @RequestMapping(value="/save",method= RequestMethod.POST)
    public JSONResult saveOrUpdate(@RequestBody Lssue lssue){
        //入库传入商品名字和商品数量 商品id
        if(lssue.getId()!=null){
            lssueService.updateById(lssue);
        }else{
            lssueService.insert(lssue);
        }
        return JSONResult.success();
    }

    /**
    * 删除对象
    */
    @RequestMapping(value="/{id}",method=RequestMethod.DELETE)
    public JSONResult delete(@PathVariable("id") Long id){
        lssueService.deleteById(id);
        return JSONResult.success();
    }

    /**
   * 获取对象
   */
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public JSONResult get(@PathVariable("id")Long id){
        return JSONResult.success(lssueService.selectById(id));
    }

    @RequestMapping(value = "/name/{name}",method = RequestMethod.GET)
    public JSONResult getByName(@PathVariable("name")String name){
        Lssue lssue = lssueService.getByName(name);
        return JSONResult.success("该商品库存为"+lssue.getCount());
    }

    /**
    * 查询所有对象
    */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public JSONResult list(){
        return JSONResult.success(lssueService.selectList(null));
    }
    @RequestMapping(value = "/list2",method = RequestMethod.GET)
    public JSONResult list2(){
        return JSONResult.success("缺货库存为"+lssueService.selectList2(number));
    }

    /**
    * 带条件分页查询数据
    */
    @RequestMapping(value = "/pagelist",method = RequestMethod.POST)
    public JSONResult page(@RequestBody LssueQuery query){
        Page<Lssue> page = new Page<Lssue>(query.getPage(),query.getRows());
        page = lssueService.selectPage(page);
        return JSONResult.success(new PageList<Lssue>(page.getTotal(),page.getRecords()));
    }
}
